cc.Class({
	extends: cc.Component,

	properties: {
		bgmVol : 0.7,
		soundVol : 1.0,
		
		bgm : {
			type : cc.AudioClip,
			default : null,
		},
	},
	onLoad: function ()
	{
		if( !cc.utils.soundMgr )
		{
			cc.utils.soundMgr = this;
			this.soundList = {};
			this.bgmList = {};
		}
		else
		{
			this.destroy();
		}
	},
	setMute : function( b ) {
		this.mute = b;
		if( this.mute ) {
			for(var key in this.bgmList )
			{
				var handle = this.bgmList[key];
				cc.audioEngine.setVolume( handle.id, 0 );
			}
			for(var key in this.soundList )
			{
				var handle = this.soundList[key];
				cc.audioEngine.setVolume( handle.id, 0 );
			}
		}
		else {
			for(var key in this.bgmList )
			{
				var handle = this.bgmList[key];
				cc.audioEngine.setVolume( handle.id, this.bgmVol * handle.volDetail );
			}
			for(var key in this.soundList )
			{
				var handle = this.soundList[key];
				cc.audioEngine.setVolume( handle.id, this.soundVol * handle.volDetail );
			}
		}
	},


	playBgm : function( name, loop, volDetail, isFade ) { 
		if( this.bgmList[name] ) return;

		if( volDetail === undefined || volDetail === null ) {
			volDetail = 1;
		}
		loop = loop || true;

		if( this[name] ){
			var id = cc.audioEngine.play( this[name], loop, this.bgmVol * volDetail );
			this.bgmList[name] = {
				id : id,
				volDetail : volDetail,
			};
			if( !loop )
			{
				cc.audioEngine.setFinishCallback( id, function() {
					delete this.bgmList[name];
				}.bind(this));
			}
			if( isFade )
			{
				var data = this.bgmList[name];
				var vol = 0;
				var tarVol = this.bgmList[name].volDetail;
				var id = this.bgmList[ name ].id;
				cc.audioEngine.setVolume( id, 0 );
				var funHandle = {};
				funHandle._fadeIn = function( dt ) {
					vol += dt;
					if( vol >= tarVol ) {
						vol = tarVol;
						cc.audioEngine.setVolume( id, this.bgmVol * vol );
						this.unschedule( funHandle._fadeIn );
						cc.log("playBgm:", vol );
					}
					else {
						cc.audioEngine.setVolume( id, this.bgmVol * vol );
					}
				}.bind(this);
				this.schedule( funHandle._fadeIn, 0.1 );
			}
		}
	},
	stopBgm : function( name, isFade ) { //isFade是否渐变。 
		var handle = this.bgmList[name];
		if( !handle ) return;


		if( !isFade )
		{
			cc.audioEngine.stop( handle.id );// handle.id
			delete this.bgmList[name];
		}
		else
		{
			delete this.bgmList[name];
			var vol = handle.volDetail;
			var id = handle.id;
			var funHandle = {};
			funHandle._fadeOut = function( dt ) {
				vol -= dt;
				if( vol <= 0 ) {
					vol = 0;
					cc.audioEngine.stop( id );// handle.id
					this.unschedule( funHandle._fadeOut );
					cc.log("stopBgm:", vol );
				}
				else {
					cc.audioEngine.setVolume( id, this.bgmVol * vol );
				}
			}.bind(this);
			this.schedule( funHandle._fadeOut,0.1 );
		}
	},
	playSound : function( name, loop, volDetail )
	{
		if( this.mute ) return;
		if( this.soundList[name] ) return;

		volDetail = volDetail || 1;
		loop = loop || false;

		if( this[name] ){
			var id = cc.audioEngine.play( this[name], loop, this.soundVol * volDetail );
			if( loop )
			{
				this.soundList[name] = {
					id : id,
					volDetail : volDetail,
				};
			}
		}
	},
	playClip : function( clip, type, loop, volDetail )
	{
		type = "sound" || "bgm";
		if( type == "sound" && this.mute ) return;
		var list;
		var vol;
		if( type === "bgm" ) {
			list = this.bgmList;
			vol = this.bgmVol;
		}
		else {
			list = this.soundList;
			vol = this.soundVol;
		}

		if( list[clip.name] ) return;

		volDetail = volDetail || 1;
		loop = loop || false;
		
		var id = cc.audioEngine.play( clip, loop, this.soundVol * volDetail );
		if( loop )
		{
			list[clip.name] = {
				id : id,
				volDetail : volDetail,
			};
		}
	},
	stopSound : function( name )
	{
		var handle = this.soundList[name];
		if( !handle ) return;
		cc.audioEngine.stop( handle.id );// handle.id

		delete this.soundList[name];
	},
	setBgmVol : function( vol, name, isFade )
	{
		if( vol === undefined ) {
			vol = this.bgmVol;
		}

		if( name )
		{
			if( !this.bgmList[name] ) {
				return;
			}
			if( !isFade ) {
				this.bgmList[name].volDetail = vol;
				var id = this.bgmList[ name ].id;
				cc.audioEngine.setVolume( id, this.bgmVol * vol );
			}
			else {
				var handle = this.bgmList[name];
				var originVol = handle.volDetail;
				var targetVol = vol;
				var id = handle.id;
				var funHandle = {};
				funHandle._fadeOut = function( dt ) {
					if( originVol > targetVol ) {
						originVol -= dt;
					}
					else if ( originVol < targetVol ){
						originVol += dt;
					}
					if( Math.abs(originVol - targetVol) < 0.05 ) {
						originVol = targetVol;
						this.unschedule( funHandle._fadeOut );
						cc.log("targetVol:", originVol );
						handle.volDetail = originVol;
					}
					else {
						cc.audioEngine.setVolume( id, this.bgmVol * originVol );
					}
				}.bind(this);
				this.schedule( funHandle._fadeOut,0.1 );
			}
		}
		else
		{
			this.bgmVol = vol;
			for(var key in this.bgmList )
			{
				var handle = this.bgmList[key];
				cc.audioEngine.setVolume( handle.id, this.bgmVol * handle.volDetail );
			}
		}
	},
	setSoundVol : function( vol, name )
	{
		if( vol === undefined ) {
			vol = this.soundVol;
		}

		if( name )
		{
			if( !this.soundList[name] ) {
				return;
			}
			this.soundList[name].volDetail = vol;
			var id = this.soundList[ name ].id;
			cc.audioEngine.setVolume( id, this.soundVol * vol );
		}
		else
		{
			this.soundVol = vol;
			for(var key in this.soundList )
			{
				var handle = this.soundList[key];
				cc.audioEngine.setVolume( handle.id, this.soundVol * handle.volDetail );
			}
		}
	}
});
